import codecs

# SPL playlist class
class SPL:
	def __init__(self):
		self.list = []

	def load(self, file):
		f = codecs.open(file, encoding='utf-16')
		l = f.readline()
		if l.strip() != u'SPL PLAYLIST':
			f.close()
			raise Exception('Invalid file. '+l.strip())
		l = f.readline()
		if l.strip() != u'VERSION 1.00':
			f.close()
			raise Exception('Invalid file. '+l.strip())
		self.list = []
		for l in f:
			l = l.strip()
			if l == u'END PLAYLIST':
				f.close()
				return
			elif len(l) != 0:
				#
				self.list.append(l)
		f.close()


	def save(self, file):
		self.list = [unicode(i, encoding='utf-8') for i in self.list]
		f = codecs.open(file, encoding='utf-16', mode='wb')
		f.write('SPL PLAYLIST\r\n')
		f.write('VERSION 1.00\r\n')
		f.write('\r\n')
		for l in self.list:
			f.write(l+u'\r\n')
		f.write('\r\n')
		f.write('END PLAYLIST\r\n')
		f.close()
		pass


